package com.group.auth;

import com.group.view.MainFrame;

import javax.swing.*;
import java.awt.*;

/**
 * @author lenovo
 */
public class LoginFrame extends JFrame {
    public LoginFrame() {
        setTitle("登录");
        setSize(600, 400);
        setLocationRelativeTo(null);
        // 窗口将会居中显示
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        // 点击关闭按钮时退出应用

        JTextField usernameField = new JTextField();
        // 账号
        JPasswordField passwordField = new JPasswordField();
        // 密码
        JLabel status = new JLabel();
        JButton loginBtn = new JButton("登录");
        JButton registerBtn = new JButton("注册");

        setLayout(new GridLayout(5, 1));
        add(new JLabel("用户名：")); add(usernameField);
        add(new JLabel("密码：")); add(passwordField);
        add(loginBtn); add(registerBtn); add(status);

        loginBtn.addActionListener(e -> {
            String user = usernameField.getText().trim();
            String pass = new String(passwordField.getPassword()).trim();
            if (AuthManager.login(user, pass)) {
                com.group.util.StudentDataLoader.loadFromFile("saveStudents.txt");
                new MainFrame().setVisible(true);
                dispose();

            } else {
                status.setText("用户名或密码错误");
            }
        });

        registerBtn.addActionListener(e -> new com.group.auth.RegisterFrame().setVisible(true));
    }
}
